
package com.nashuju.web.controller.login;

import java.util.Date;
import org.durcframework.core.MessageResult;
import org.durcframework.core.UserContext;
import org.durcframework.core.ValidateHolder;
import org.durcframework.core.controller.BaseController;
import org.durcframework.core.util.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nashuju.web.common.RMSContext;
import com.nashuju.web.entity.sw.RUser;
import com.nashuju.web.entity.user.User;
import com.nashuju.web.service.role.RoleService;
import com.nashuju.web.service.sw.RUserService;
import com.nashuju.web.service.sw.STaskService;
import com.nashuju.web.service.user.UserService;
import com.nashuju.web.util.PasswordUtil;

@Controller
public class LoginController extends BaseController {

    @Autowired
    private UserService rUserService;

    @Autowired
    private RUserService sUserService;;
    
    @Autowired
    private STaskService sTaskService;
    
    @Autowired
    private RoleService roleService;

    /**
     * 用户登陆
     * 
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "login.do" )
    public @ResponseBody MessageResult login(User backUser) {
    	if (STaskService.count == 0) {
    		sTaskService.showTimer();
		}

        ValidateHolder validateHolder = ValidateUtil.validate(backUser);
        if (validateHolder.isSuccess()) {
            if (StringUtils.hasText(backUser.getUsername())) {
                User user = rUserService.getByUserName(backUser.getUsername());
                RUser rUser = sUserService.get(user.getId());
                if (user == null) {
                    return error("用户名密码不正确");
                }

                String password = backUser.getPassword();
                String correctHash = user.getPassword();

                boolean isPswdCorrect = PasswordUtil.validatePassword(password, correctHash);

                if (isPswdCorrect) {

                	doLogin(user);
                    String roleNumber = null;
                    try {
                    	roleNumber = ""+roleService.getUserRole(user.getUsername()).get(0).getRoleId();
					} catch (Exception e) {
						roleNumber = "23";
					}
                    return success(roleNumber);
                }
            }
        }


        return error("用户名密码不正确");
    }
    
    

    private void doLogin(User user) {
        // 缓存当前用户角色权限
        RMSContext.getInstance().refreshUserRightData(user.getUsername());

        user.setLastLoginDate(new Date());
        UserContext.getInstance().setUser(user);
        try {
        	 rUserService.update(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
        rUserService.update(user);
    }

    /**
     * 注销
     * 
     * @return
     */
    @RequestMapping("logout.do")
    public @ResponseBody MessageResult logout() {
        RMSContext.getInstance().clearCurrentUserRightData();
        UserContext.getInstance().setUser(null);
        return success();
    }

}
